home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / netvis / SocketThreads.cpp < prev    next >
C/C++ Source or Header  |  2001-05-02  |  5KB  |  177 lines

  1. #include "netvis.h"
  2. #include "ZHLTThread.h"
  3.  
  4. NetvisSession*      g_ClientSession = NULL; // The client socket when g_visstate == VIS_MODE_CLIENT
  5. NetvisSocketServer* g_SocketServer = NULL;  // The server socket server when g_visstate == VIS_MODE_SERVER
  6.  
  7. class PingThread : public ZHLTThread
  8. {
  9. protected:
  10.     virtual void Run()
  11.     {
  12.         hlassert(g_vismode == VIS_MODE_CLIENT)
  13.  
  14.         while (_zhlt_isCancelled() == false)
  15.         {
  16.             // This fixes stalls, as well as keeping the clients updated with portals 
  17.             // from the other clients while working on a time consuming portal
  18.             if ((g_visstate == VIS_PORTAL_FLOW) && outOfWork())
  19.             {
  20.                 Sleep(1000);
  21.                 if (stillOutOfWork() || needServerUpdate())
  22.                 {
  23.                     Send_VIS_WANT_FULL_SYNC();
  24.                 }
  25.             }
  26.             if (g_visstate >= VIS_DONE)
  27.             {
  28.                 return;
  29.             }
  30.             Sleep(1000);
  31.         }
  32.     }
  33. };
  34.  
  35.  
  36. class StatusDisplayThread : public ZHLTThread
  37. {
  38. protected:
  39.     virtual void Run()
  40.     {
  41.         while (_zhlt_isCancelled() == false)
  42.         {
  43.             if ((g_vismode == VIS_MODE_CLIENT) && g_ClientSession)
  44.             {
  45.                 g_ClientSession->DisplayClientStats();
  46.             }
  47.             else if ((g_vismode == VIS_MODE_SERVER) && g_SocketServer)
  48.             {
  49.                 g_SocketServer->DisplayServerStats();
  50.             }
  51.  
  52.             for (unsigned x = 0; ((x < m_RateSeconds) && (_zhlt_isCancelled() == false)); x++)
  53.             {
  54.                 Sleep(1000);
  55.             }
  56.         }
  57.     }
  58.  
  59. public:
  60.     StatusDisplayThread(unsigned int rateseconds) // in SECONDS
  61.     {
  62.         m_RateSeconds = rateseconds;
  63.     }
  64.  
  65.     unsigned int    m_RateSeconds;
  66. };
  67.  
  68.  
  69. static PingThread*          s_PingThread = NULL;
  70. static StatusDisplayThread* s_StatusDisplayThread = NULL;
  71.  
  72.  
  73. bool isConnectedToServer()
  74. {
  75.     bool rval = false;
  76.     hlassert(g_vismode == VIS_MODE_CLIENT);
  77.     if (g_ClientSession != NULL)
  78.     {
  79.         rval = g_ClientSession->m_Active;
  80.     }
  81.     return rval;
  82. }
  83.  
  84.  
  85. void ConnectToServer(const char* addr, short port)
  86. {
  87.     InetHostAddress ia = addr;
  88.  
  89.     try
  90.     {
  91.         g_ClientSession = new NetvisSession(ia, port);
  92.         g_ClientSession->Start();
  93.         StartPingThread();
  94.     }
  95.     catch (Socket* socket)
  96.     {
  97.         Error("Failed to connect to server %s on port %d", addr, port);
  98.     }
  99. }
  100.  
  101. void DisconnectFromServer()
  102. {
  103.     StopStatusDisplayThread();
  104.     StopPingThread();
  105.     if (g_ClientSession)
  106.     {
  107.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "DisconnectFromServer: waiting on socket thread to finish\n"));
  108.         delete g_ClientSession;
  109.         g_ClientSession = NULL;
  110.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "DisconnectFromServer: done waiting on socket thread to finish\n"));
  111.     }
  112. }
  113.  
  114. void StartNetvisSocketServer(short port)
  115. {
  116.     g_SocketServer = new NetvisSocketServer(port);
  117.     g_SocketServer->Start();
  118. }
  119.  
  120. void StopNetvisSocketServer()
  121. {
  122.     StopStatusDisplayThread();
  123.     if (g_SocketServer)
  124.     {
  125.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopNetvisSocketServer: waiting on socket server thread to finish\n"));
  126.         delete g_SocketServer;
  127.         g_SocketServer = NULL;
  128.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopNetvisSocketServer: done waiting on socket server thread to finish\n"));
  129.     }
  130. }
  131.  
  132. void StartPingThread()
  133. {
  134.     s_PingThread = new PingThread();
  135.     s_PingThread->Start();
  136. }
  137.  
  138. void StopPingThread()
  139. {
  140.     if (s_PingThread)
  141.     {
  142.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopPingThread: waiting on ping thread to finish\n"));
  143.         delete s_PingThread;
  144.         s_PingThread = NULL;
  145.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopPingThread: done waiting ping thread to finish\n"));
  146.     }
  147. }
  148.  
  149. void StartStatusDisplayThread(unsigned int rateseconds)
  150. {
  151.     // Rate passed in is in seconds, thread expects milliseconds
  152.     s_StatusDisplayThread = new StatusDisplayThread(rateseconds);
  153.     s_StatusDisplayThread->Start();
  154. }
  155.  
  156. void StopStatusDisplayThread()
  157. {
  158.     if (s_StatusDisplayThread)
  159.     {
  160.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopStatusDisplay: waiting on status display thread to finish\n"));
  161.         delete s_StatusDisplayThread;
  162.         s_StatusDisplayThread = NULL;
  163.         IfDebug(Developer(DEVELOPER_LEVEL_MESSAGE, "StopStatusDisplay: done waiting on status display thread to finish\n"));
  164.     }
  165. }
  166.  
  167.  
  168. void NetvisSleep(unsigned long amt)
  169. {
  170. #ifdef SYSTEM_WIN32
  171.     Sleep(amt);
  172. #endif
  173. #ifdef SYSTEM_POSIX
  174.     usleep(amt * 1000);
  175. #endif
  176. }
  177.